package com.ql.dao;

import com.ql.domain.Device;
import com.ql.domain.Project;
import com.ql.domain.Section;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface SectionDao {
    /**
     * 根据userId查询对应的标段
     *
     * @param userId
     * @return
     */
    @Select("select * from section where id in (select sectionId from users_section where userId=#{userId})")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "sectionName", property = "sectionName"),
            @Result(column = "constructionUnit", property = "constructionUnit"),
            @Result(column = "inspectUnit", property = "inspectUnit"),
            @Result(column = "supervisionUnit", property = "supervisionUnit"),
            @Result(column = "orderBy", property = "orderBy"),
            @Result(column = "state", property = "state"),
            @Result(column = "introduce", property = "introduce"),
            @Result(column = "project_id", property = "project",
                    javaType = Project.class,
                    one = @One(select = "com.ql.dao.ProjectDao.findById"))
    })
    List<Section> findByUserId(String userId);

    /**
     * 根据deviceId查询对应标段
     *
     * @param deviceId
     * @return
     */
    @Select("select * from section where id=#{deviceId}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "sectionName", property = "sectionName"),
            @Result(column = "constructionUnit", property = "constructionUnit"),
            @Result(column = "inspectUnit", property = "inspectUnit"),
            @Result(column = "supervisionUnit", property = "supervisionUnit"),
            @Result(column = "orderBy", property = "orderBy"),
            @Result(column = "state", property = "state"),
            @Result(column = "introduce", property = "introduce"),
            @Result(column = "project_id", property = "project_id"),
            @Result(column = "project_id", property = "project",
                    javaType = Project.class,
                    one = @One(select = "com.ql.dao.ProjectDao.findById"))
    })
    List<Section> findByDeviceId(String deviceId);


    /**
     * 查询全部项目+sectionName调价查询
     *
     * @return
     */
    List<Section> findAll(String sectionName);

    /**
     * 添加标段
     *
     * @param section
     */
    @Insert("insert into section(sectionName,constructionUnit,inspectUnit,supervisionUnit,orderBy,state,introduce,project_id) values(#{sectionName},#{constructionUnit},#{inspectUnit},#{supervisionUnit},#{orderBy},#{state},#{introduce},#{project_id})")
    void save(Section section);

    /**
     * 更新标段
     *
     * @param section
     */
    void update(Section section);

    /**
     * 根据Id删除标段
     *
     * @param id
     */
    @Delete("delete from section where id=#{id}")
    void del(String id);

    //    @Select("select * from section where state =0 order by orderBy ASC")

    /**
     * 根据projectId获取关联的开启的标段信息
     *
     * @param projectId
     * @return
     */
   /* @Select("<script>" +
            "select * from section " +
            "<where>" +
            "state =0" +
            "<if test=\"_parameter!=null and _parameter!=''\">" +
            " and project_id = #{projectId}" +
            "</if>" +
            "</where>" +
            "order by orderBy ASC" +
            "</script>")*/
    List<Section> sectionGroupNameByProjectId(@Param("projectId") String projectId,@Param("userId")String userId);

    /**
     * 根据userId获取关联的开启的标段信息
     *
     * @param userId
     * @return
     */
    @Select("select * from section where id in (select sectionId from users_section where userId=#{userId}) and state =0  order by orderBy ASC")
    List<Section> getSectionGroupName(String userId);

    /**
     * 根据用户id查询出所有的设备
     * @param userId
     * @return
     */
    @Select("select * from section where id in (select sectionId from users_section where userId=#{userId}) and state = 0")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "sectionName", property = "sectionName"),
            @Result(column = "constructionUnit", property = "constructionUnit"),
            @Result(column = "inspectUnit", property = "inspectUnit"),
            @Result(column = "supervisionUnit", property = "supervisionUnit"),
            @Result(column = "orderBy", property = "orderBy"),
            @Result(column = "state", property = "state"),
            @Result(column = "introduce", property = "introduce"),
            @Result(column = "id", property = "devices",
                    javaType = List.class,
                    many = @Many(select = "com.ql.dao.DeviceDao.findDeviceIdsBySectionId"))
    })
    List<Section> findDeviceByUserId(String userId);

/*    @Delete("delete from users_section where  sectionId=#{sectionId}")
    void delUserSectionBySectionId( @Param("sectionId") String sectionId);*/

    /**
     * 根据项目id查询出所有关联的设备
     *
     * @param projectId
     * @return
     */
    @Select("select * from section where project_id =#{projectId}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "sectionName", property = "sectionName"),
            @Result(column = "constructionUnit", property = "constructionUnit"),
            @Result(column = "inspectUnit", property = "inspectUnit"),
            @Result(column = "supervisionUnit", property = "supervisionUnit"),
            @Result(column = "orderBy", property = "orderBy"),
            @Result(column = "state", property = "state"),
            @Result(column = "introduce", property = "introduce"),
            @Result(column = "id", property = "devices",
                    javaType = List.class,
                    many = @Many(select = "com.ql.dao.DeviceDao.findDeviceIdsBySectionId"))
    })
    List<Section> findByProjectId(String projectId);

    /**
     * 根据标段id查询出所有关联的设备
     *
     * @param sectionId
     * @return
     */
    @Select("select * from section where id =#{sectionId}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "sectionName", property = "sectionName"),
            @Result(column = "constructionUnit", property = "constructionUnit"),
            @Result(column = "inspectUnit", property = "inspectUnit"),
            @Result(column = "supervisionUnit", property = "supervisionUnit"),
            @Result(column = "orderBy", property = "orderBy"),
            @Result(column = "state", property = "state"),
            @Result(column = "introduce", property = "introduce"),
            @Result(column = "id", property = "devices",
                    javaType = List.class,
                    many = @Many(select = "com.ql.dao.DeviceDao.findDeviceIdsBySectionId"))
    })
    List<Section> findBySectionId(String sectionId);

}
